#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
using namespace std;
#define N 1024
int main()
{
    int piped[2]={0};
    int ans=pipe(piped);
    if(!ans){
        cout<<"piped[0]=="<<piped[0]<<endl;
        cout<<"piped[1]=="<<piped[1]<<endl;
    }

    int id=fork();
    //子写 父读
    if(id==0){
        //子进程
        close(piped[0]);
        char msg[]="hello father";
        int cnt=5;
        while(cnt--){
            write(piped[1],msg,strlen(msg));
        }
        close(piped[1]);
        exit(0);
    }
    //父进程
    close(piped[1]);
    while(1){
        char buffer[N];
        int ret=read(piped[0],buffer,sizeof(buffer));
        if(ret>0){
            cout<<"我是父："<<buffer<<endl;
        }
    }
    close(piped[0]);
    return 0;
}